package com.markerhub.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.common.lang.retunData;
import com.markerhub.entity.TFacilityBase;
import com.markerhub.entity.TFacilityFunction;
import com.markerhub.entity.TFacilityReqair;
import com.markerhub.entity.TProjectBase;
import com.markerhub.povo.TFacilityReqairVo;
import com.markerhub.service.TFacilityBaseService;
import com.markerhub.service.TFacilityReqairService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘佳明
 * @since 2021-12-30
 */
@Controller
@RequestMapping("/tFacilityReqair")
public class TFacilityReqairController {

    @Autowired
    TFacilityReqairService tFacilityReqairService;
    @Autowired
    TFacilityBaseService tFacilityBaseService;

    @RequestMapping("/list")
    @PreAuthorize("hasAuthority('sys:reqair:list')")
    @ResponseBody
    public retunData list(@Param("page") Integer page,@Param("limit") Integer limit, @Param("number") String number, @Param("projectid") String projectid) {
        Integer pages=(page-1)*limit;
        retunData retunData = new retunData();

        System.out.println(page);
        System.out.println(limit);
        System.out.println(number);
        System.out.println(projectid);
        if (page==0&&limit==0){
            List<TFacilityReqair> list = tFacilityReqairService.list();
            retunData.setData(list);
            retunData.setCount(list.size());
            return retunData;
        }
        List<TFacilityReqairVo> pageData = tFacilityReqairService.getReqairList(pages, limit,number,projectid);
        List<TFacilityReqairVo> pageData1 = tFacilityReqairService.getReqairList(0, 0,number,projectid);
        retunData.setData(pageData);
        retunData.setCount(pageData.size());
        retunData.setSize(pageData1.size());
        return retunData;
    }
    // 新增项目信息
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('sys:reqair:add')")
    @ResponseBody
    public  Result add(String number ,String phone ,String reqairDescribe ,Integer reqairType ,Integer ID){
       // System.out.println(TFacilityReqairVo);
        System.out.println(ID);
        System.out.println(number);
        System.out.println(reqairDescribe);
        System.out.println(reqairType);
        System.out.println(phone);
        TFacilityReqairVo tFacilityReqairVo = new TFacilityReqairVo();
        List<TFacilityBase> tFacilityBases = tFacilityBaseService.list(new QueryWrapper<TFacilityBase>().eq("number", number));
        if (tFacilityBases.size()>0){
            tFacilityReqairVo.setReqairTime(LocalDateTime.now());
            tFacilityReqairVo.setUserobject(ID);
            tFacilityReqairVo.setReqairStatus(0);
            tFacilityReqairVo.setPhone(phone);
            tFacilityReqairVo.setReqairDescribe(reqairDescribe);
            tFacilityReqairVo.setReqairType(reqairType);
            tFacilityReqairVo.setBaseid(tFacilityBases.get(0).getId());

            boolean save = tFacilityReqairService.save(tFacilityReqairVo);
            return  Result.succ(tFacilityReqairVo);
        }
        return  Result.fail("没有找到该编号的设备信息！！！");

    }

    ///  派遣维修
    @RequestMapping("/updateReqair")
    @PreAuthorize("hasAuthority('sys:reqair:updateReqair')")
    @ResponseBody
    public  Result updateReqair(String reqairId ,String reqairDescribe ,Integer reqairStatus){
        // System.out.println(TFacilityReqairVo);

        TFacilityReqairVo tFacilityReqairVo = new TFacilityReqairVo();
        List<TFacilityReqair> tFacilityBases = tFacilityReqairService.list(new QueryWrapper<TFacilityReqair>().eq("reqair_id", reqairId));
        if (tFacilityBases.size()>0){
            tFacilityBases.get(0).setReqairDescribe(reqairDescribe);
            tFacilityBases.get(0).setReqairStatus(reqairStatus);
            boolean save = tFacilityReqairService.updateById(tFacilityBases.get(0));
            return  Result.succ(tFacilityReqairVo);
        }
        return  Result.fail("没有找到该编号的设备信息！！！");

    }

    // 删除
    @RequestMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:reqair:delete')")
    @ResponseBody
    @Transactional
    public  Result delete(@PathVariable("id") String id){
        System.out.println("sys:reqair:delete === >>"+id);
        boolean delete = tFacilityReqairService.removeById(id);
      //  boolean id1 = tFacilityFunctionService.remove(new QueryWrapper<TFacilityFunction>().eq("baseid", id));
        return Result.succ(null);
    }

}

